package com.fp.m7.server.helpers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fp.m7.api.model.Comment;
import com.fp.m7.shared.dto.CommentDto;
import com.fp.m7.shared.dto.UserDto;

/**
 * Factory for creating commentDtos.
 * 
 * @author 7
 */
@Component
public class CommentDtoFactory {

    @Autowired
    private UserDtoFactory userDtoFactory;

    public CommentDto build(Comment comment) {
        UserDto userDto = userDtoFactory.build(comment.getAuthor());
        return new CommentDto(comment.getId(), comment.getIssuedDate(), comment.getContent(), userDto);
    }

    public List<CommentDto> build(List<Comment> comments) {
        List<CommentDto> rtrn = new ArrayList<CommentDto>(comments.size());
        for (Comment comment : comments) {
            if (comment != null) {
                rtrn.add(build(comment));
            }
        }
        return rtrn;
    }

    public void setUserDtoFactory(UserDtoFactory userDtoFactory) {
        this.userDtoFactory = userDtoFactory;
    }
}
